<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$return=array();
$worker = pdo_get('jy_peiorder_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
if(!$worker['id'] || $worker['status'] !=1){
	exi('','error','/apply/');
}
if($_RQ['op']=='main'){
	$sendtypes=array(
		'1'=>'同城配送',
		'2'=>'到店自取',
	);
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>0);
	if($_RQ['merchid']){
		$this->session('merchid',$_RQ['merchid']);
		$groupspram['merchid']=$_RQ['merchid'];
		$return['merch']=pdo_get('jy_merch_user',array('id'=>$_RQ['merchid'],'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','address','lat_lng'));
		$sendtypes=model('jy_merch/model')->sendtypes($_RQ['merchid'],$sendtypes);
		if(count($sendtypes)==1){
			$sendtype=$sendtypes[1]?1:2;
		}
	}else{
		$groupspram['mid']=0;
	}
	if($parentid){
		$return['pid']=0;
	}
	$category=pdo_getall('jy_weishop_goods_group',$groupspram,array(),'','paixu desc');
	$return['category']=$category;
	$return['post']=array(
		'merchid'=>$_RQ['merchid'],
		'sendtype'=>$sendtype?:1,
		'paytype'=>1,
		'goods'=>array(),
		'peiset'=>array('days'=>'','goods'=>array('k'=>0)),
	);
	$return['params']=array(
		'pid'=>'-1',
		'merchid'=>$_RQ['merchid'],
		'nowmonth'=>date('Y-m'),
		'work_weekarray'=>array('日','一','二','三','四','五','六'),
	);
	$merchs=pdo_getall('jy_merch_user',array('status'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','address','lat_lng'));	
	foreach($merchs as &$mv){
		$mv['lat_lng']=json_decode($mv['lat_lng'],true);
		$mv['sendtypes']=model('jy_merch/model')->sendtypes($mv['id'],$sendtypes);
		if(count($mv['sendtypes'])==1){
			$mv['sendtype']=$mv['sendtypes'][1]?1:2;
		}else{
			$mv['sendtype']=1;
		}
	}
	$return['merchs']=$merchs;
	$return['sendtypes']=$sendtypes;
	//设置时间
	$return['currentyear'] =$_RQ['year']?:date('Y');
	$return['currentmonth'] = $_RQ['month']?:date('m');
	if($_SESSION['onemerchid']){
		$return['yuesetting']=$this->plugin_setting('yuesetting'.$_SESSION['onemerchid']);
		$return['yuyuexieyi']=$this->plugin_setting('yuyuexieyi'.$_SESSION['onemerchid']);
	}else{
		$return['yuesetting']=$this->plugin_setting('yuesetting');
		$return['yuyuexieyi']=$this->plugin_setting('yuyuexieyi');
	}
	$this->session('peisetgoods','');
	$return['demonumset'] = array(
			array('num'=>1),
			array('num'=>1),
			array('num'=>1),
			array('num'=>1),
			array('num'=>1),
			array('num'=>1),
			array('num'=>1),
	);
	exi($return);
}
if($_RQ['op']=='getcategory'){
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>0);
	if($_RQ['merchid']){
		$groupspram['merchid']=$_RQ['merchid'];
		$this->session('merchid',$_RQ['merchid']);
	}else{
		$groupspram['mid']=0;
	}
	if($parentid){
		$return['pid']=0;
	}
	$category=pdo_getall('jy_weishop_goods_group',$groupspram,array(),'','paixu desc');
	$return['list']=$category;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['mobile'] || !$postdata['realname']){
			exi('请填写客户姓名和电话！','error');
	}
	if(!$postdata['mid']){
			exi('请填写客户会员ID！','error');
	}
	if(!$postdata['pcd'] || !$postdata['address']){
			exi('请填写地址！','error');
	}
		
	if(!$postdata['goods'][0]['id']){
			exi('请选择商品！','error');
	}
	$address=array(
			'pcd'=>$postdata['pcd'],
			'address'=>$postdata['address'],
			'mobile'=>$postdata['mobile'],
			'realname'=>$postdata['realname'],
	);
	$postdata['peiset']['goods']=$this->session('peisetgoods');
	$new_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$postdata['mid'],
			'merchid'=>$postdata['merchid'],
			'goodsprice'=>$postdata['goodsprice'],
			'price'=>$postdata['price'],
			'dispatchprice'=>$postdata['dispatchprice'],
			'remark'=>trim($postdata['remark']),
			'goods'=>ijson_encode($postdata['goods']),
			'sendtype'=>$postdata['sendtype']?:1,
			'paytype'=>$postdata['paytype']?:1,
			'mobile'=>$postdata['mobile'],
			'realname'=>$postdata['realname'],
			'address'=>ijson_encode($address),
			'peisong_id'=>$worker['id'],
			'peiset'=>ijson_encode($postdata['peiset']),
			'createtime'=>time(),
	);
	$this->insert('baodan',$new_order);
	exi('提交成功','','/index/');
}
if($_RQ['op']=='postgoods'){
	$post=get_postdata();
	$goods=$post;
	foreach($goods as $k=>$gv){
		if($gv['num']){
			//
		}
	}
	$this->session('peisetgoods',$goods);
	exi('提交成功');
}
if($_RQ['op']=='time_days'){
	$order_id=$this->session('order_id');
		$year =$_RQ['year']?:date('Y');
		$month = $_RQ['month']?:date('m');
		$day = date("t",strtotime($year.'-'.$month));
		//$day = cal_days_in_month(CAL_GREGORIAN, $month,$year);
		$calendar = array();
		$i = 1;
		$goods=$this->session('peisetgoods');
		$goodsnum=array();
		foreach($goods as $k=>$gv){
			$goodsnum[$k]=$gv['goodsnum']?:$gv['total'];
		}
		while ($i <= $day) {
				$key=$i;
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date=$year . '-' . $month . '-' . $i;
				if(strtotime($date)<time()){
					$islast=1;
				}else{
					$islast=0;
				}
				if(!$islast){
					$week = date('w', strtotime($date));
					if($goods){
						$daygoods=array();
						foreach($goods as $key=>&$gv){
							//商品
							if($goodsnum[$k]<=0){
								continue;
							}
							if($gv['numset'][$week]['num']>0){
								$gv['title']=cfc('tools')->substr($gv['title'],0,5);
								$gv['num']=$gv['numset'][$week]['num'];
								$goodsnum[$k]-=$gv['numset'][$week]['num'];
								$daygoods[]=$gv;
							}else{
								continue;
							}
							
						}
					}
				}
				$calendar[] = array(
					'i'=>$i,
					'day'=>$date,
					'islast'=>$islast,
					'goods'=>$daygoods
				);
				++$i;
		}
		$return['calendar'] = $calendar;
		$weekday=$year . '-' . $month . '-01';
		$return['week'] = date('w', strtotime($weekday));
		$return['kongday']=array();
		for($i=0;$i<$return['week'];$i++){
			$return['kongday'][]=$i;
		}
		
		$years = array();
		$i = 0;
		while ($i <= 10) {
			$years[] = $year + $i;
			++$i;
		}
		$months = array('01','02','03','04','05','06','07','08','09','10','11','12');
		$return['months']=$months;
		$return['years']=$years;
		$return['year']=$year;
		$return['month']=$month;
		exi($return);
}
?>